<template>
	<view class="wrap bg-white">
		<view class="cu-bar bg-white">
			<u-button type="primary" class="no-border confirm-btn" @click="clear" >清空识别码</u-button>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view class="padding">
			<textarea  :focus="firstFocus" class=""  placeholder="请扫识别码!" placeholder-style="font-size:28rpx" maxlength="-1"  v-model="cbarCode" @input="search" rows="10"/>
		</view>
		<view class="">
			<view class="u-border padding">
				<text class="text-bold">{{cinvname}}:</text>
				<text class="text-bold text-red">共 {{inum}}件 {{iqty}}罐</text>
			</view>
		</view>
		<view  style="height: 65px;" ></view>
		<view class="footer">
			<view class="confrim-btn padding padding-bottom-sm flex bg-white">
				<u-button type="success" class="no-border confirm-btn"  @click="tofrom">{{qty}}/{{num}}</u-button>
				<!-- <u-button type="error" class="no-border confirm-btn" @click="suberror">{{wnum}}</u-button> -->
				<u-button type="warning" class="no-border confirm-btn"  @click="subyf">{{yqty}}/{{ynum}}</u-button>
				<u-button type="primary" class="no-border confirm-btn" @click="submit" :disabled='isab'>确认</u-button>
			</view>
		</view>
		<js-error mode="bottom" ref="jsError"></js-error>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				CustomBar: this.CustomBar,
				size:-1,
				inum:0,
				iqty:0,
				num:0,
				wnum:0,
				ynum:0,
				qty:0,
				wqty:0,
				yqty:0,
				isyx:false,
				show:false,
				id:'',
				ccode:'',
				cinvcode:'',
				cinvname:'',
				cbarCode:'',
				list:[], //成功
				wlist:[], //无码
				ylist:[], //已发
				firstFocus: false,
				xlist:[], //箱码过滤list
				isab:false,
				refList:[],
			}
		},
		onLoad(data) {
			this.id = data.id;
			this.ccode = data.ccode;
			this.cinvcode = data.cinvcode;
			this.cinvname = data.cinvname;
			this.inum = data.inum;
			this.iqty = data.qty;
			this.iinvexchrate = data.iinvexchrate == undefined?6:data.iinvexchrate;
			var _this = this;
			setTimeout(() => {
				this.firstFocus = true
			},0)
		},
		methods: {
			//调用错误页面
			suberror(){
				var _this = this;
				uni.navigateTo({
				    url: '/pages/btdm/index/smck/smerror/smerror',
					events: {
					   // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					   Filter(data) {
							_this.addWlist(data);
					   }
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						res.eventChannel.emit('initfilter', _this.wlist);
					}
				})
			},
			//分装错误数据
			addWlist(data){
				this.wqty = data;
			},
			
			//调用已发页面
			subyf(){
				var _this = this;
				uni.navigateTo({
				    url: '/pages/zgz/smck/smyf/smyf',
					events: {
					   // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					   Filter(data) {
							_this.addyf(data);
					   }
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						res.eventChannel.emit('initfilter', _this.ylist);
					}
				})
			},
			
			//分装错误数据
			addyf(data){
				this.yqty  = 0;
				data.forEach(res=>{
					this.yqty = parseInt(this.yqty) + parseInt(res.quty);
				})
				this.ylist = data;
				this.ynum = this.ylist.length;
				this.isyx = true;
			},
			
			tofrom(){
				var _this = this;
				uni.navigateTo({
				    url: '/pages/zgz/smsc/from/from',
					events: {
					   // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					   Filter(data) {
						   _this.list = data;
						   _this.count = data.length;
						   _this.num = data.length == undefined?0:data.length;
						   _this.qty = _this.num * _this.iinvexchrate;
						   console.log('iinvexchrate',_this.qty,_this.num)
					   }
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						res.eventChannel.emit('initfilter', _this.list);
					}
				})
			},
			
			/** 发生声音*/
			sendMp3(name){
				console.log("=====testClick=====");
				let src = '/static/jeesite/'+name+'.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			
			/** 清空条码*/
			clear(){
				this.firstFocus = false;
				this.cbarCode = "";
				setTimeout(() => {
					this.firstFocus = true;
				},10)
			},
			
			/** 获取数据*/
			getData(code){
				var _this = this;
				console.log(code,'code',_this.ccode)
				if(code != '' && (code.length == 24 || code.length == 11)){
					var index = _this.list.findIndex((item) => item.code == code)
					if (index == -1) {
						var query = {
							"cbarcode":code
						}
						_this.$u.api.box.getBoxQty(query).then(res=>{
							if(res.result == 'true' ){
								if(res.data.fhQuty == undefined || res.data.fhQuty == 0){
									console.log('成功')
									_this.sendMp3('cg');
									var r = {
										code:code,
										quty:parseInt(res.data.quty)
									}
									_this.list.push(r);
									_this.num = parseInt(_this.num) + 1;
									_this.qty = parseInt(_this.qty) +  parseInt(res.data.quty);
									console.log('_this.qty',_this.qty)
								}else if(res.data.ckCode != _this.ccode){//已发
								console.log('已发')
									_this.sendMp3('yf');
									this.isyx = false;
									var i = _this.ylist.findIndex((item) => item.code == code)
									if(i == -1){
										var r = {
											code:code,
											quty:parseInt(res.data.quty)
										}
										_this.ylist.push(r);
										_this.ynum = parseInt(_this.ynum) + 1;
										_this.yqty = parseInt(_this.yqty) +  parseInt(res.data.quty);
									}
								}else{
									console.log('sb')
									_this.sendMp3('sb');
								}
							}else{ //无码
								_this.sendMp3('bcz');
							}
						});
					}else{
						_this.sendMp3('sb');
					}
				}else if(code != '' && ((code.indexOf("=") > -1 &&  code.split("=")[1] != '' && code.split("=")[1].length == 20) || code.length == 12)){
					code = code.split("=")[1];
					var index = _this.list.findIndex((item) => item.code == code)
					if (index == -1) {
						_this.sendMp3('cg');
						var res = {
							code:code
						}
						_this.list.push(res)
						_this.qty = parseInt(_this.qty) + 1;
						console.log(_this.qty,'qty')
					}else{
						_this.sendMp3('sb');
					}
				}else{
					if(code != ''){
						_this.sendMp3('sb');
					}
				}
			},
			
			/** 扫码查询*/
			search(){
				var _this = this;
				var code = this.$u.trim(_this.cbarCode.split("\n")[_this.cbarCode.split("\n").length - 1],'all');
				if(_this.refList.findIndex((item) => item == code) == -1){
					_this.refList.push(code);
					this.getData(code);
				}
			},
			
			/** 扫码 */
			toScan() {
				// #ifdef APP-PLUS
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module");
				mpaasScanModule.mpaasScan({
					// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
					'scanType': ['qrCode', 'barCode'],
					// 是否隐藏相册，默认false不隐藏
					'hideAlbum': false,
					//ios需要设置这个参数，只支持中英文 zh-Hans、en，默认中文                       
					'language': 'en',
					//相册选择照片识别错误提示(ios)
					'failedMsg': '未识别到二维码，请重试',
					//Android支持全屏需要设置此参数
					'screenType': 'full'
				},
					async (ret) => {
						console.log('ret', ret)
						if (ret.resp_message == 'success') {							
							var code = ret.resp_result;
							this.getData(code);
						}
					})
				// #endif
				// #ifdef H5 || MP-WEIXIN || MP-QQ 
				var _this = this;
				uni.scanCode({ 
					onlyFromCamera: true,
					success: function(res) {
						var code = res.result;
						this.getData(code);
					}.bind(this)
				});
				// #endif
			},
			
			//页面删除数据
			delData(code){
				var _this = this;
				var list = _this.list;
				for(var i = 0 ; i < list.length ; i++ ){
					if(list[i].code == code){
						if(list[i].code.length == 24){
							_this.num = parseInt(_this.num) - 1;
							_this.qty = parseInt(_this.qty) - parseInt(list[i].quty);
						}else if(list[i].code.length == 20){
							_this.qty = parseInt(_this.qty) - 1;
						}
						list.splice(i,1);
					}
				}
			},
			//提交
			submit(){
				this.isab = true;
				var _this = this;
				this.firstFocus = false
				var data = JSON.stringify(this.list);//成功数据
				var ix = 0;
				var er = {};
				var wnum = 0;
				var ynum = 0;
				if(this.wqty != '' && this.wqty > 0){
					er = {
						list:_this.wlist,
						qty:_this.wqty
					}
					wnum = _this.wnum;
					ix = Number.parseInt(ix) + Number.parseInt(_this.wqty);
				}
				var erdata = JSON.stringify(er);//无码数据
				var yfdata = '';
				if(this.ylist.length > 0 ){
					if(this.isyx){
						yfdata = JSON.stringify(this.ylist);//已发数据
						this.ylist.forEach(item=>{
							ix = Number.parseInt(ix) + Number.parseInt(item.quty);
						})
						ynum = _this.ynum;
					}else{
						_this.$refs.jsError.showError('','请将不存在识别码和已发识别码处理后提交!','error');
					}
				}
				ix = Number.parseInt(ix) + Number.parseInt(this.qty);
				var parm = {
					"id":this.id, //出库单id
					"data":data,    //成功数据
					"cinvcode":this.cinvcode,
					"erdata":erdata,
					"yfdata":yfdata,
					"ccode":this.ccode, //出库单号
				}
				console.log(ix,this.iqty,'-----')
				if(ix <= this.qty){
					this.$u.api.box.saveRdr09(parm).then(res=>{
						this.isab = false;
						this.cbarCode = "";
						this.scode = "";
						if(res.result == 'true'){
							_this.list = [];
							_this.ylist = [], //已发
							//wlist:[], //无码
							_this.$u.toast(res.message);
							if(ix == _this.iqty){
								uni.navigateBack();
							}else{
								_this.iqty = Number.parseInt(_this.iqty) -  Number.parseInt(ix); //刷新数量
								_this.inum = Number.parseInt(_this.inum) -  Number.parseInt(_this.num);//箱数
								_this.inum = Number.parseInt(_this.inum) - Number.parseInt(wnum);//箱数
								_this.inum = Number.parseInt(_this.inum) - Number.parseInt(ynum);//箱数
								_this.num= 0;
								_this.wnum = 0;
								_this.ynum = 0;
								_this.qty = 0;
								_this.wqty = 0;
								_this.yqty = 0;
								
							}
						}else{
							_this.$refs.jsError.showError('',res.message,'error');
							return;
						}
					})
				}else{
					_this.$refs.jsError.showError('','请按照规定数量出库!','error');
				}
				setTimeout(() => {
					this.firstFocus = true
				},10)
			},
		}
	}
</script>

<style>
@import '../btdm.css';
.scroll-list {
	height: calc(100vh - var(--window-top) - var(--window-bottom) - 415px);
	width: 100%;
}
</style>
